public class MyLinkedList implements SingleLinkedList{
//    无头单向非循环链表实现

    public class ListNode{
        public int val;
        public ListNode next;

        public ListNode(int val){
            this.val = val;
        }

    }

    public ListNode head;

    @Override
    public void addFirst(int data) {
        ListNode node = new ListNode(data);
        //当头插时 没有节点
        if(head == null){
            head = node;
            return;
        }

        //正常情况 下 头插
        node.next = head;
        head = node;
    }

    @Override
    public void addLast(int data) {
        ListNode node = new ListNode(data);

        //当尾插时 没有节点
        if(head == null){
            head = node;
            return;
        }

        ListNode cur = head;
        //正常情况下尾插
        while(cur.next != null){
            cur = cur.next;
        }
        //  走到这里 已经在最后一个节点
        cur.next = node;

    }

    @Override
    public void addIndex(int index, int data) {
        //在指定位置插入数据
        // 首先要判断 index的合法性
        try{
            checkIndex(index);
        }catch(IndexIsNotLegal e){
            e.printStackTrace();
        }

        //走到此处 index 已经合法
        if(index == 0){
            addFirst(data);
            return;
        }

        if(index == size()){
            addLast(data);
            return;
        }

        //走到此处 index 已经可以是 正常 插入场景
        ListNode node = new ListNode(data);

        ListNode cur = head;

        for(int i = 0; i < index-1;i++){
            cur = cur.next;
        }

        //此处 已经到 index 前一个位置
        node.next = cur.next;
        cur.next = node;
        return;

    }

    public void checkIndex(int index) throws IndexIsNotLegal{
        if(index > size()||index < 0){
            throw new IndexIsNotLegal("index位置不合法");
        }
    }

    @Override
    public boolean contains(int key) {
        if(head == null){
            return false;
        }
        ListNode cur = head;
        while(cur != null){
            if(cur.val == key){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    @Override
    public void remove(int key) {
        ListNode cur = head;
        ListNode prev = cur;

        //如果移除的是头节点
        if(head.val == key){
            head = head.next;
            return;
        }

        //正常情况下的 移除
        while(cur != null){
            if(cur.val == key){
                cur = cur.next;
                prev.next = cur;
                return;
            } else {
                prev = cur;
                cur = cur.next;
            }
        }

    }

    @Override
    public void removeAllKey(int key) {
        ListNode prev = head;
        ListNode cur = head;


        while(cur != null){
            if(cur.val == key){
                prev.next = cur.next;
                cur = cur.next;
            } else {
                prev = cur;
                cur = cur.next;
            }
        }

        if(head.val == key){
            head = head.next;
        }
    }

    @Override
    public int size() {
        if(head == null){
            return 0;
        }

        int count = 0;
        ListNode cur = head;
        while(cur != null){
            cur = cur.next;
            count++;
        }

        return count;
    }

    @Override
    public void display() {
        ListNode cur = head;
        while(cur != null){
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    @Override
    public void clear() {

    }
}
